import tkinter as tk from tkinter import scrolledtext from pynput import mouse, keyboard import threading import pyperclip class KeyMouseTracker: def __init__(self): self.root = tk.Tk() self.root.title("Key and Mouse Tracker") self.key_label = tk.Label(self.root, text="Last Key Pressed:") self.key_label.pack() self.mouse_label = tk.Label(self.root, text="Mouse Movement:") self.mouse_label.pack() self.mouse_x_label = tk.Label(self.root, text="X:") self.mouse_x_label.pack() self.mouse_x_entry = tk.Entry(self.root) self.mouse_x_entry.pack() self.mouse_y_label = tk.Label(self.root, text="Y:") self.mouse_y_label.pack() self.mouse_y_entry = tk.Entry(self.root) self.mouse_y_entry.pack() self.change_mouse_button = tk.Button(self.root, text="Change Mouse Position", command=self.change_mouse_position) self.change_mouse_button.pack() self.capture_key_var = tk.BooleanVar() self.capture_key_var.set(False) self.capture_key_checkbox = tk.Checkbutton(self.root, text="Capture Key Presses", variable=self.capture_key_var) self.capture_key_checkbox.pack() self.capture_click_var = tk.BooleanVar() self.capture_click_var.set(False) self.capture_click_checkbox = tk.Checkbutton(self.root, text="Capture Mouse Clicks", variable=self.capture_click_var) self.capture_click_checkbox.pack() self.capture_mouse_pos_var = tk.BooleanVar() self.capture_mouse_pos_var.set(False) self.capture_mouse_pos_checkbox = tk.Checkbutton(self.root, text="Capture Mouse Movements", variable=self.capture_mouse_pos_var) self.capture_mouse_pos_checkbox.pack() self.click_label = tk.Label(self.root, text="Last Click Position:") self.click_label.pack() self.copy_click_position_button = tk.Button(self.root, text="Copy Last Click Position", command=self.copy_second_last_click_position) self.copy_click_position_button.pack() self.click_x_label = tk.Label(self.root, text="X:") self.click_x_label.pack() self.click_x_entry = tk.Entry(self.root) self.click_x_entry.pack() self.click_y_label = tk.Label(self.root, text="Y:") self.click_y_label.pack() self.click_y_entry = tk.Entry(self.root) self.click_y_entry.pack() self.click_button = tk.Button(self.root, text="Click at Specific Position", command=self.click_at_position) self.click_button.pack() self.scrollbar = tk.Scrollbar(self.root, orient=tk.VERTICAL) self.history_text = scrolledtext.ScrolledText(self.root, height=10, width=40, wrap=tk.WORD, yscrollcommand=self.scrollbar.set) self.history_text.pack(side=tk.LEFT) self.scrollbar.config(command=self.history_text.yview) self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y) self.clear_history_button = tk.Button(self.root, text="Clear History", command=self.clear_history) self.clear_history_button.pack() self.keyboard_listener = keyboard.Listener(on_press=self.on_key_event) self.mouse_listener = mouse.Listener(on_move=self.on_mouse_move, on_click=self.on_mouse_click) self.history = [] def on_key_event(self, key): if self.capture_key_var.get(): self.key_label.config(text=f"Last Key Pressed: {key}") self.history.append(f"Key Pressed: {key}") self.update_history() def on_mouse_move(self, x, y): if self.capture_mouse_pos_var.get(): self.mouse_label.config(text=f"Mouse Movement: x={x}, y={y}") self.history.append(f"Mouse Movement: x={x}, y={y}") self.update_history() def on_mouse_click(self, x, y, button, pressed): if self.capture_click_var.get() and pressed: self.click_label.config(text=f"Last Click Position: x={x}, y={y}") self.history.append(f"Mouse Clicked: Button {button} at x={x}, y={y}") self.update_history() def change_mouse_position(self): try: x = int(self.mouse_x_entry.get()) y = int(self.mouse_y_entry.get()) # Handle the special case for moving the mouse off the screen if x == -1 or y == -1: mouse.Controller().position = (None, None) self.history.append("Mouse Moved off the screen") else: mouse.Controller().position = (x, y) self.history.append(f"Mouse Position Changed: x={x}, y={y}") self.update_history() except ValueError: pass def click_at_position(self): try: x = int(self.click_x_entry.get()) y = int(self.click_y_entry.get()) mouse.Controller().position = (x, y) mouse.Controller().click(mouse.Button.left) self.history.append(f"Mouse Clicked at Specific Position: x={x}, y={y}") self.update_history() except ValueError: pass def copy_second_last_click_position(self): if len(self.history) >= 2 and "Mouse Clicked:" in self.history[-2]: second_last_click_position = self.history[-2].split("at")[1].strip() pyperclip.copy(second_last_click_position) self.history.append(f"Second Last Click Position Copied to Clipboard: {second_last_click_position}") self.update_history() def update_history(self): self.history_text.config(state=tk.NORMAL) self.history_text.delete(1.0, tk.END) for item in self.history[-10:]: # Display the last 10 items in the history self.history_text.insert(tk.END, f"{item}\n") self.history_text.config(state=tk.DISABLED) def clear_history(self): self.history = [] self.update_history() def start_listeners(self): self.keyboard_listener.start() self.mouse_listener.start() self.root.mainloop() def stop_listeners(self): self.keyboard_listener.stop() self.mouse_listener.stop() def start_app(): tracker = KeyMouseTracker() tracker.start_listeners() if __name__ == "__main__": threading.Thread(target=start_app).start()